Skip to content

Conversation

mikew
Copy link
Contributor

@mikew mikew commented Jun 22, 2025

This makes it possible for people to use the CLI plugin in tandem with the single-instance plugin:

.plugin(tauri_plugin_single_instance::init(|app, args, _cwd| {
  let matches = app.cli().matches(Some(args));
  match matches {
    Ok(matches) => {
      // Do something with matches
    }
    Err(e) => {
      eprintln!("Error parsing CLI arguments: {}", e);
    }
  }
}))

Note it's still a little awkward to actually use. If people want to send the matches to the client, they need to encode / decode them themselves as emit_to expects the data to be clone-able.

eg Here's what I have to do on the Rust side:

let serialized = serde_json::to_string(&matches).unwrap();
let _ = app.emit_to("main", "cli", serialized);

and on the client side:

listen<string>('cli', async (event) => {
  const matches: CliMatches = JSON.parse(event.payload)

  // Do something with matches
})

Closes #2785

Copy link
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also do you mind also add a change file like this one?

Added a new `global` boolean flag to the `CliArg` struct to support global CLI arguments. This flag allows arguments like `--verbose` to be marked as global and automatically propagated to all subcommands, enabling consistent availability throughout the CLI.

Copy link
Contributor

github-actions bot commented Jun 22, 2025

Package Changes Through 9763dc2

There are 2 changes which include cli with minor, cli-js with minor

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
api-example 2.0.28 2.0.29
api-example-js 2.0.24 2.0.25
cli 2.2.1 2.3.0
cli-js 2.2.1 2.3.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@mikew
Copy link
Contributor Author

mikew commented Jun 22, 2025

Also do you mind also add a change file like this one?

Added a new `global` boolean flag to the `CliArg` struct to support global CLI arguments. This flag allows arguments like `--verbose` to be marked as global and automatically propagated to all subcommands, enabling consistent availability throughout the CLI.

Can maybe scrounge together a docs update too if you think it would help!

@Legend-Master
Copy link
Contributor

Legend-Master commented Jun 22, 2025

About the clone trait thing, I don't mind we add derive Clone for Matches and its fields, just add it to #[derive(Default, Debug, Serialize)] like this #[derive(Default, Debug, Serialize, Clone)]

Comment on lines +82 to +86
pub fn get_matches(
cli: &Config,
package_info: &PackageInfo,
args: Option<Vec<String>>,
) -> crate::Result<Matches> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Legend-Master Curious and slightly bored here, hope you don't mind me asking. You called out the changes to Cli.matches as a breaking change, how is this also not considered a breaking change? Both Cli.matches and parser::get_matches are public, and neither are referenced anywhere in the usage guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, did not notice that the docs do mention Cli.matches.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser is a private module, so parser::get_matches is not actually public, yeah I know it's kinda confusing 😂

@mikew
Copy link
Contributor Author

mikew commented Jun 22, 2025

About the clone trait thing, I don't mind we add derive Clone for Matches and its fields, just add it to #[derive(Default, Debug, Serialize)] like this #[derive(Default, Debug, Serialize, Clone)]

I tried that in my testing but Rust then complained about Matches.args not being clone-able:

Diagnostics:
1. the trait bound `ArgData: Clone` is not satisfied
   required for `HashMap<std::string::String, ArgData>` to implement `Clone` [E0277]
Diagnostics:
1. the trait bound `SubcommandMatches: Clone` is not satisfied
   required for `Box<SubcommandMatches>` to implement `Clone`
   1 redundant requirement hidden
   required for `std::option::Option<Box<SubcommandMatches>>` to implement `Clone` [E0277]

@Legend-Master
Copy link
Contributor

I tried that in my testing but Rust then complained about Matches.args not being clone-able:

You'll need to also add it to SubcommandMatches and ArgData

Copy link
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Legend-Master Legend-Master merged commit f6e1128 into tauri-apps:v2 Jun 22, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use tauri-plugin-cli with tauri-plugin-single-instance
2 participants